<html>
<head><meta charset="utf-8"><title>lifetime in const-generic · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html">lifetime in const-generic</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202851206"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202851206" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yodal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202851206">(Jul 04 2020 at 02:05)</a>:</h4>
<p>I've been looking into issue <a href="https://github.com/rust-lang/rust/issues/60814">#60814</a>. The current error comes about because the const-arg is borrow-checked within <code>use_it</code> while <code>'a</code> is only found in <code>function_with_string</code>. I figure this should be handled the same way as normal function arguments, but I will admit I don't know where or how lifetimes are handled for function arguments. Does this seem like the right direction to head in for this, and if so where are lifetimes for function arguments handled?</p>



<a name="202862443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202862443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202862443">(Jul 04 2020 at 07:53)</a>:</h4>
<p>I am personally in favor of forbidding lt in const generics (forever) <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> </p>
<p>Afaict we have the same problem as <a href="https://github.com/rust-lang/rust/pull/71154">https://github.com/rust-lang/rust/pull/71154</a> here. I.e. to infer <code>'a</code> we (in general) require the value of <code>STRING</code>, which once again relies on the value of <code>'a</code>.</p>



<a name="202862484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202862484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202862484">(Jul 04 2020 at 07:54)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="cp">#![feature(const_generics)]</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">function_with_str</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">STRING</span>: <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">str</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">give_str</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="na">&#39;a</span><span class="w"> </span><span class="kt">str</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="s">&quot;Hello, world!&quot;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">use_it</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">function_with_str</span>::<span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">give_str</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="c1">// How do we want to typeck `{ give_str() }` here?</span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">use_it2</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">function_with_str</span>::<span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">give_str</span>::<span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Do we want to infer the lt of `function_with_str` from its generic arg here?</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="202862642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202862642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202862642">(Jul 04 2020 at 07:59)</a>:</h4>
<p>I might be over complicating something here though <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span> </p>
<p>cc <span class="user-mention" data-user-id="121053">@varkor</span> considering that you were active on <a href="https://github.com/rust-lang/rust/issues/60814">#60814</a>, what are your proposed semantics here?</p>



<a name="202862949"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202862949" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202862949">(Jul 04 2020 at 08:04)</a>:</h4>
<blockquote>
<p>I figure this should be handled the same way as normal function arguments</p>
</blockquote>
<p>Maybe, we could also consider checking <code>{ give_str() }</code> as <code>fn ANON&lt;'a&gt;() -&gt; &amp;'a str { give_str() }</code>, meaning that we require it to be valid for all lifetimes, which at least prevents the cyclic dependency <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="202863158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202863158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202863158">(Jul 04 2020 at 08:11)</a>:</h4>
<p>This would cause a compilation error  in <code>use_it2</code> though but I can't think of use cases which would end up being impossible due to this</p>



<a name="202866240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202866240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yodal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202866240">(Jul 04 2020 at 09:51)</a>:</h4>
<p>Honestly, I would not be against forbidding lifetimes other than <code>'static</code> seeing as I cannot think of any time they would even make sense. We don't allow non-<code>'static</code> values in any other const context, do we?</p>



<a name="202866360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202866360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202866360">(Jul 04 2020 at 09:54)</a>:</h4>
<p>One fairly interesting case would be something like this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">test</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">VALUE</span>: <span class="nc">std</span>::<span class="n">mem</span>::<span class="n">Discriminant</span><span class="o">&lt;</span><span class="n">Enum</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;&gt;</span><span class="p">(</span><span class="n">v</span>: <span class="nc">Enum</span><span class="o">&lt;</span><span class="na">&#39;a</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">discriminant</span><span class="p">(</span><span class="o">&amp;</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">VALUE</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="202866425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202866425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202866425">(Jul 04 2020 at 09:56)</a>:</h4>
<p><code>mem::Discriminant</code> is invariant so <code>VALUE</code> has to be <code>mem::Discriminant</code> of <code>Enum&lt;'a&gt;</code> and can't use <code>Enum&lt;'static&gt;</code>.</p>



<a name="202866716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202866716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yodal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202866716">(Jul 04 2020 at 10:05)</a>:</h4>
<p>Hmm, that definitely seems like a valid use</p>



<a name="202866868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202866868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202866868">(Jul 04 2020 at 10:09)</a>:</h4>
<p>hmm, let's also cc <span class="user-mention" data-user-id="119009">@eddyb</span> and <span class="user-mention" data-user-id="116009">@nikomatsakis</span> here, I think it's probably a good idea to get some rough plan on how we want to deal with generic lt bounds in const generic params before spending too much time on getting something to work</p>



<a name="202866883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202866883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202866883">(Jul 04 2020 at 10:09)</a>:</h4>
<p>I think it would be easiest to completely forbid them (at least for now) as we should always be able to add them back in without breaking back compat</p>



<a name="202867284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202867284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yodal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202867284">(Jul 04 2020 at 10:21)</a>:</h4>
<p>I'll look at adding an error message that points to an issue about lifetimes in const generics so that the ICE can get squashed in the short term. It shouldn't be too much work to get that done.</p>



<a name="202904482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/lifetime%20in%20const-generic/near/202904482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> yodal <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/lifetime.20in.20const-generic.html#202904482">(Jul 05 2020 at 04:21)</a>:</h4>
<p>An issue has been created for this issue (<a href="https://github.com/rust-lang/rust/issues/74052">#74052</a>) and a PR to forbid non-<code>'static</code> lifetimes has been created (<a href="https://github.com/rust-lang/rust/issues/74051">#74051</a>)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>